import 'dart:convert';

import "package:flutter/material.dart";
import 'package:shared_preferences/shared_preferences.dart';
import 'package:word_range_client/widgets/profile/Features.dart';
// 组件
import 'package:word_range_client/widgets/profile/TopButtons.dart';
import 'package:word_range_client/widgets/profile/UserDate.dart';
import 'package:word_range_client/widgets/profile/UserProfile.dart';

import '../../pojo/vo/UserVo.dart';
import '../../widgets/appInfo/AppInfoFooter.dart';
import '../../widgets/profile/ActivityIntegral.dart';

class Profile extends StatefulWidget {
  const Profile({super.key});

  @override
  State<Profile> createState() => _PhoneProfileState();
}

class _PhoneProfileState extends State<Profile> {
  String name = "";
  String signature = "";
  String avatarUrl = "";

  @override
  void initState() {
    super.initState();
    // 组件初始化时尝试解析用户信息
    _updateUserData();
  }

  Future<void> _updateUserData() async {
    final prefs = await SharedPreferences.getInstance();
    final userJson = prefs.getString("currentUser");
    if (userJson != null) {
      UserVo user = UserVo.fromJson(json.decode(userJson));
      setState(() {
        name = user.name!;
        signature = user.signature?? "这人很懒，没留下签名";
        avatarUrl = user.avatarUrl?? "";
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ConstrainedBox(
          constraints: const BoxConstraints(maxWidth: 900),
          child: Column(
            children: [
              const TopButtons(),
              Card(
                elevation: 6,
                shape: RoundedRectangleBorder(              // 形状
                    borderRadius: BorderRadius.circular(15)   // 圆角
                ),
                margin: const EdgeInsets.all(10),
                color: Theme.of(context).colorScheme.primary,
                child: Container(
                  padding: const EdgeInsets.all(10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      UserProfile(
                        userName: name,
                        userAvatarUrl: avatarUrl,
                        userSignature: signature,
                      ),
                      const SizedBox(height: 10,),
                      const UserData(),
                    ],
                  ),
                ),
              ),
              // 积分卡片
              const ActivityIntegral(),
              const Expanded(
                  child: Features()
              ),
              const AppInfoFooter()
            ],
          ),
        ),
      ),
    );
  }
}
